package engine.newUi
{
	import engine.data.ModelLocator;
	import engine.display.newAttack.AttackLogic;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.scene.SceneManager;
	
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import newx.com.config.StaticIni;

	public class FactionAttackLogic
	{
		private static var _self 			:FactionAttackLogic;
		private var _ui					:FactionAttackUI;
		private var _model					:ModelLocator;
		//存放战报数据
		private var _warVector				:Vector.<AttackWarVo>;
		//帮派战时间
		private var _date					:Date;
		private var _time					:Timer;
		private var _state					:int;	//状态   1 准备状态  2战斗状态 
		private var _leftFaction			:String = "";
		private var _rightFaction			:String = "";
		
		private var _bindindex				:int    =-1;
		
		/**准备状态**/
		public const READY_STATE			:int	=1;
		/**战斗状态**/
		public const ATTACK_STATE			:int	=2;
		public static function getInstance():FactionAttackLogic
		{
			if(_self == null)
			{
				_self = new FactionAttackLogic();
			}
			return _self;
		}
		public function FactionAttackLogic()
		{
		}
		public function Init() : void
		{
			_date		= new Date();
			_time		= new Timer(1000);
			_warVector	= new Vector.<AttackWarVo>();
			_ui			= FactionAttackUI.getInstance();
			_model		= ModelLocator.getInstance();
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_GUILD_FIGHT_REST_ALIVE_NUM),getFactionMsg);
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_GUILD_FIGHT_FIGHT_TIMER),getStateMsg);
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_GUILD_FIGHT_SINGLE_FIGHT_INFO),getWarMsg);
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_SEND_FIGHT_GUILD),getFaction);
		}
	
		public function	BeforeShow():void
		{
			addEvent();
		}
		public function	AfterHide():void
		{
			removeEvent();
		}
		private function addEvent():void
		{
			_time		.addEventListener(TimerEvent.TIMER,			onPlayer);
			_ui.exitBtn.addEventListener(MouseEvent.CLICK,			onClick);
		}
		private function removeEvent():void
		{
			_time		.removeEventListener(TimerEvent.TIMER,			onPlayer);
			_ui.exitBtn.removeEventListener(MouseEvent.CLICK,		onClick);
		}
		private function getFaction(e:CustomMsgEvent):void
		{
			_leftFaction = e.pList[1];
			_rightFaction= e.pList[2];
		}
		/**
		 * 帮派信息 
		 * @param e
		 * 
		 */		
		private function getFactionMsg(e:CustomMsgEvent = null):void
		{
			var factionInfo:Array =e.pList;
			updateLabel(factionInfo);

		}
		/**
		 * 帮派战状态 
		 * @param e
		 * 
		 */		
		private function getStateMsg(e:CustomMsgEvent):void
		{
			_state = e.pList[0];
			if(_state==1)
			{
				_ui.warText.htmlText="";
			}
			
			_date.time =e.pList[1]*1000;
			if(_date.time>0)
			{
				_time.start();
			}
			SceneManager.getInstance().FactionBattleWay();
		}
		/**
		 * 战报信息 
		 * @param e
		 * 
		 */		
		private function getWarMsg(e:CustomMsgEvent =null):void
		{
			if(_ui.created == false)
			{
				_ui.showOrHide();
			}
			_ui.visible =!AttackLogic.getInstance().fighting;
			var arr:Array =e.pList;
			var type:int  =arr.shift();   // 1 进入战场 2 退出战场 3战报
			var name:String="";
			switch(type)
			{
				case 1:
					name =arr.shift();
					break;
				case 2:
					name =arr.shift();
					break;
				case 3:
					getWarInfo(arr);
					break;
			}
			showWar(type,name);
		}
		private function setMC(val1:int,val2:int):void
		{
			_ui.leftMC.visible  = false;
			_ui.rightMC.visible = false;
			if(val1 >val2)
			{
				_ui.leftMC.visible = true;
			}
			else if(val1<val2)
			{
				_ui.rightMC.visible = true;
			}
		}
		private function updateLabel(arr:Array):void
		{
			var leftFaction:Array =[arr[1],arr[0],arr[2]];
			var rightFaction:Array =[arr[4],arr[3],arr[5]];
			_leftFaction = leftFaction[1];
			_rightFaction= rightFaction[1];
			if(arr[0]== "")
			{
				_ui.leftLabel.htmlText ="";
			}
			else
			{
				_ui.leftLabel.htmlText =StaticIni.getSwfLangStrVar("UIString_1000163",leftFaction);
			}
			
			if(arr[3] == "")
			{
				_ui.rightLabel.htmlText ="";
			}
			else
			{
				_ui.rightLabel.htmlText=StaticIni.getSwfLangStrVar("UIString_1000164",rightFaction);
			}
			
			setMC(leftFaction[2],rightFaction[2]);
			
			SceneManager.getInstance().FactionBattleWay();
		}
		private function getWarInfo(arr:Array):void
		{
			_warVector=new Vector.<AttackWarVo>();
			for(var i:int=0;i<arr.length;i+=4)
			{
				_warVector.push(new AttackWarVo(arr[i],arr[i+1],arr[i+2],arr[i+3]));
			}
		}
		/**
		 *显示战报 
		 * 
		 */		
		private function showWar(type:int,name :String=""):void
		{
			var str:String="";
		
			 if(type == 3)
			 {
					str = byVoSetStr(_warVector[0])+"<br>";
			 }
			 else
			 {
				 str =setBattleStr(type,name);
			 }
		
			_ui.warText.htmlText += str;
		}
		private function setBattleStr(type:int,name:String):String
		{
			var strID:String;
			if(type == 1)
			{
				strID ="UIString_1000168";
			}
			else
			{
				strID ="UIString_1000169";
			}
			return StaticIni.getSwfLangStrVar(strID,[name]);
		}
		/**
		 * 设置战报字符串 
		 * @param vo 战报
		 * @return 
		 * 
		 */		
		private function byVoSetStr(vo:AttackWarVo):String
		{
			var strID:String;
			if(vo.faction1 == _leftFaction)
			{
				strID = "UIString_1000154";
			}
			else
			{
				strID = "UIString_1000155";
			}
			var str:String =StaticIni.getSwfLangStrVar(strID,[vo.name1,vo.name2]);
			return str;
		}
		/**
		 * 转换时间 
		 * @param _date
		 * @return 
		 * 
		 */		
		private function getTimeStr(_date:Date):Array
		{
			var minutes:int  = _date.minutesUTC;
			var seconds:int  = _date.secondsUTC;
			var arr:Array   = [initTime(minutes),initTime(seconds)];
			return arr;	
		}
		/**
		 * 格式时间 
		 * @param val
		 * @return 
		 * 
		 */		
		private function initTime(val:int):String
		{
			var  va:String = val.toString();
			val>9?va:va="0"+va;
			return va;
		}
		/**
		 * 左阵营 帮派 
		 * @return 
		 * 
		 */		
		public function get LeftFaction():String
		{
			return _leftFaction;
		}
		/**
		 * 右阵营 帮派 
		 * @return 
		 * 
		 */		
		public function get RihtFaction():String
		{
			return _rightFaction;
		}
		/**
		 * 帮派战的 状态   
		 * @return 
		 * 
		 */		
		public function get BattleState():Boolean
		{
			if(_state == READY_STATE)
			{
				return false;
			}
			else if(_state == ATTACK_STATE)
			{
				return true;
			}
			return false;
		}
		/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		private function onClick(e:MouseEvent):void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_REQUEST_GOTO_FIGHT_SCENE,2);
		}
		private function onPlayer(e:TimerEvent):void
		{
			_date.time -=1000;
			var arr:Array=getTimeStr(_date);
			var strID:String;
			if(_state == 1)
			{
				strID = "GuildUIStr_29";
			}
			else
			{
				strID = "GuildUIStr_30";
			}
			_ui.timeLabel.htmlText	= StaticIni.getSwfLangStrVar(strID,arr)+"";
			if(_date.time<=0)
			{
				_time.reset();
			}
		}
	}
}
class AttackWarVo
{
	
	public var name1:String;  //胜利名字
	public var faction1:String;		//胜利帮
	public var name2:String;	//失败名字
	public var faction2:String //失败帮派
	public function AttackWarVo(name1:String,faction1:String,name2:String,faction2:String):void
	{
		this.faction1	= faction1;
		this.name1 = name1;
		this.name2 = name2;
		this.faction2 = faction2;
	}
}